'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 英雄技能
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var moment = require('moment');
var mongoose = require('mongoose');
var skillItemSchema = require('./skill/skillItemSchema');
var dataApi = require('../libs/dataApi');
var skillConf = require('../config/skillConf');
var skillMgr = require('../mgr/skillMgr');
var skillConf = require('../config/skillConf');

///////////////////////////////////////////////////////////
/**
 * 英雄技能
 */
var skillSchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 技能列表
    skill_list: [skillItemSchema],

    // 战斗选择的技能
    select_skill_list: [Number],

    // 技能战力
    skill_power: {
        type: Number,
        default: 0
    },

    // 出战技能战力
    select_skill_power: {
        type: Number,
        default: 0
    }
});

skillSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

skillSchema.methods.initSkillList = function (job_type) {
    var self = this;
    var select_skill_list = skillConf.getJobCanOpenSkillList(job_type, 1);
    _.each(select_skill_list, function (skill_id) {
        self.addSkill(skill_id);
    });
};

// 添加技能
skillSchema.methods.addSkill = function (skill_type) {
    // 判断技能是否存在
    if (!dataApi.hero_skill_lvl.findById(skill_type)) {
        return false;
    }
    var skill = this.getSkill(skill_type);
    if (!!skill) {
        return false;
    }
    this.skill_list.push({
        skill_type: skill_type
    });
    return true;
};

// 获取技能
skillSchema.methods.getSkill = function (skill_type) {
    for (var i = 0; i < this.skill_list.length; i++) {
        var skill = this.skill_list[i];
        if (skill.skill_type == skill_type) {
            return skill;
        }
    }
    return null;
};

/**
 * 获取技能列表
 * @return {[type]} [description]
 */
skillSchema.methods.getSkillList = function () {
    return this.skill_list;
};

// 设置选择的技能列表
skillSchema.methods.setSelectSkillList = function (select_list) {
    var self = this;
    var select_skill_list = [];
    _.some(select_list, function (select_skill_type) {
        if (!self.getSkill(select_skill_type)) {
            return;
        }
        select_skill_list.push(select_skill_type);
    });

    // 选择技能最大个数
    select_skill_list = _.uniq(select_skill_list);
    if (select_skill_list.length <= 3) {
        this.select_skill_list = select_skill_list;
        return true;
    }
    return false;
};

// 获取选择的技能列表
skillSchema.methods.getSelectSkillList = function () {
    return this.select_skill_list;
};

/**
 * 升级技能
 */
skillSchema.methods.upgradeSkill = function (skill_type) {
    var skill = this.getSkill(skill_type);
    if (!!skill) {
        skill.upgrade();
        return true;
    }
    return false;
};

/**
 * 获取修改
 * @return {[type]} [description]
 */
skillSchema.methods.getLastChange = function () {
    var json_data = this.toJSON();
    var skill_list = [];
    _.each(this.skill_list, function (skill_item) {
        if (skill_item.isModified()) {
            skill_list.push(skill_item);
        }
    });
    json_data.skill_list = skill_list;
    return json_data;
};

/**
 * 推送给用户
 * @return {[type]} [description]
 */
skillSchema.methods.push = function () {
    skillMgr.sendSkillChanged(
        this.role_id,
        this.toJSON(),
        function (err, fails) {
            if (err) {
                console.error('send skill message error: %j, fail ids: %j', err, fails);
                return;
            }
        }
    );
};

skillSchema.methods.pushModify = function () {
    skillMgr.sendSkillChanged(
        this.role_id,
        this.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send skill message error: %j, fail ids: %j', err, fails);
                return;
            }
        }
    );
};

if (!skillSchema.options.toJSON) {
    skillSchema.options.toJSON = {};
}
/* jshint unused:false */
skillSchema.options.toJSON.transform = function (doc, ret) {
    delete ret.role_id;
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Skill', skillSchema);
